package cmd

import (
	"fmt"
	"os"
	"text/tabwriter"

	"git-todo/internal/git"
	"git-todo/internal/storage"
)

func List() {
	// 获取分支信息
	branch, err := git.GetCurrentBranch()
	if err != nil {
		fmt.Println("不在 Git 仓库中")
		return
	}

	// 获取待办任务数据
	todos, err := storage.LoadTodos()
	if err != nil {
		fmt.Println("加载待办任务数据失败:", err)
		return
	}

	tasks := todos[branch]
	if tasks == nil {
		fmt.Println("当前分支没有待办任务")
		return
	}

	writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
	fmt.Fprintln(writer, "ID\t状态\t任务")
	fmt.Fprintln(writer, "--\t----\t----")

	for _, task := range tasks {
		status := "未完成"
		if task.Done {
			status = "已完成"
		}
		fmt.Fprintf(writer, "%d\t%s\t%s\n", task.ID, status, task.Content)
	}

	writer.Flush()
	fmt.Printf("\n总计 %d 个待办任务\n", len(tasks))
}
